<?php
//if(!defined('IN_ADAGPS'))
//{
//    die(SYSTEM_ACCESS_DENIED);
//}
//=======================================================
global $objTemplate, $arrConfig;
initPage();
     $objXajax->registerFunction("saveStatus");  

 function initPage(){ 
     
 global $objDbSelect,$lang,$strResult; 
    $sql = "select power_type_id,name_$lang as name from tbl_power_type where active = 1 order by power_type_id " ;
    $arrName = $objDbSelect->GetArray($sql) ; 
    $count = count($arrName);
    $strTypeId = '';
    $arrId = array();
    $countWidth = $count+2;
    $width=round(100/$countWidth);
    $widthFirst = 100-$width*$count;
    
    
    $strResult = '<table width="100%" border="1px"><tr><td align="center" width="'.$widthFirst.'%"> '.LOAIQUYEN.'</td>';  
    for($i=0;$i<$count;$i++){
       $strTypeId .= ','.$arrName[$i]['power_type_id'];
       $arrId[]= $arrName[$i]['power_type_id'];
       $strResult .=  '<td align="center" width="'.$width.'%">'.$arrName[$i]['name'].'</td>';       
    }
   // $strResult .= '</tr>';
    $strTypeId = substr($strTypeId,1);
    if($strTypeId!=''){
       $sqlnext = "select power_type_main,power_type_sub,flg from tbl_power_create_user 
       where power_type_main in ($strTypeId ) 
       and power_type_sub in ($strTypeId) order by power_type_main,power_type_sub"; 
       $arrDetail = $objDbSelect->GetArray($sqlnext) ;      
       $countDetail = count($arrDetail);
       $tempIDmain =0; 
      $index= 0; 
      for($j=0;$j<$countDetail;$j++){
        $disable = '';
          if($tempIDmain != $arrDetail[$j]['power_type_main']){
             $strResult .='</tr><tr><td align="center" >'.$arrName[$index]['name'].'</td>';
             $tempIDmain = $arrDetail[$j]['power_type_main'];
             $index++; 
          }
          $idCheck = $arrDetail[$j]['power_type_main'].'_'.$arrDetail[$j]['power_type_sub'];
          $checked = '';
          if($arrDetail[$j]['flg']==1){
             $checked= "checked='checked'"; 
          }
          if($tempIDmain==$arrDetail[$j]['power_type_sub']||$index==0){
              $disable = "disabled='disabled'";
          }
          $strResult.="<td align='center'><input onclick='fn_checkedchange(\"$idCheck\");' name='chkCreatePower' $disable type='checkbox' id='$idCheck'  $checked > </td>";         
      }      
      $strResult.='</tr></table>';  
    }                
}

function saveStatus($strUpdate){
global $objDbUpdate;
$objResponse = new xajaxResponse();
$sqldelete = 'DELETE  FROM tbl_power_create_user ';
$sqlupdate = "INSERT INTO tbl_power_create_user(power_type_main,power_type_sub,flg) VALUES $strUpdate";
if($objDbUpdate->Execute($sqldelete)){
  if(!$objDbUpdate->Execute($sqlupdate)){
      $objResponse->addAlert('Cập Nhật Thất bại');
      return    $objResponse->getXML();  
  } 
}else{
    $objResponse->addAlert('Cập Nhật Thất bại');
    return    $objResponse->getXML();  
}
$objResponse->addAlert('Cập Nhật Thành Công');
return    $objResponse->getXML();  

}


?>